<template>
  <nav>
    <router-link to="/">Home</router-link> |
    <router-link to="/about">About</router-link> |
    <router-link to="/list">list</router-link> |
     <router-link to="/demo/2">vue3的语法糖</router-link>
  </nav>
  <!-- <keep-alive v-slot="{Component}">
     <router-view />
  </keep-alive> -->
  <!-- :include  可以绑定数组  字符串  正则 -->
  <!-- 在vue3中需要 v-slot -->
  <router-view v-slot="{Component}">
     <keep-alive :include="/HomeView|about/">
      <component :is="Component"></component>
     </keep-alive>
  </router-view>
</template>
<script>
import {ref,reactive,toRefs,watch,watchEffect} from 'vue'
import {useRoute} from 'vue-router'
export default {
    setup(props) {
        const data =reactive({
            whiteList:[],//存放需要缓存的组件name
        })
       
        const route = useRoute()
        // 监听路由的变化
        watch(()=>route,(newVal,oldVal)=>{
            console.log(newVal);
            // 把路由信息里面有keepAlive=true  并且 不能重复往whiteList里面添加
            if(newVal.meta.keepAlive && data.whiteList.indexOf(newVal.name)==-1){
                data.whiteList.push(newVal.name)
            }
            console.log(data.whiteList);
        },{deep:true})
        
        return {...toRefs(data)}
    }
}
</script>
<style lang="scss">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

nav {
  padding: 30px;

  a {
    font-weight: bold;
    color: #2c3e50;

    &.router-link-exact-active {
      color: #42b983;
    }
  }
}
</style>
